//https://leetcode.cn/problems/happy-number/
package codeRandomThoughts.Test202快乐数;

import java.util.HashSet;

//二刷
public class Solution2 {
    public boolean isHappy(int n) {
        HashSet<Integer> hashSet = new HashSet<>();
        while (true){
            n=getSquareSum(n);
            if (n==1){
                return true;
            }
            if (!hashSet.add(n)){
                return false;
            }
        }
    }


    public int getSquareSum(int num) {
        int sum = 0;
        int n = 0;//个位数
        while (num != 0) {
            n = num % 10;
            sum += n * n;
            num = num / 10;
        }
        return sum;
    }
}
